package com.zhanglifan.action;

import com.opensymphony.xwork2.ModelDriven;
import com.zhanglifan.domain.Module;
import com.zhanglifan.service.ModuleService;
import com.zhanglifan.util.Page;

import java.util.List;

/**
 * Information
 * Author: ZhangLifan
 * Time: 2017/9/24 22:24
 * Description: ModuleAction
 */
public class ModuleAction extends BaseAction implements ModelDriven<Module> {
    private Module module = new Module();
    private Page<Module> page = new Page<>();
    private ModuleService moduleService;


    public String list(){
        moduleService.findPage("from Module",page,Module.class,null);
        page.setUrl("moduleAction_list");
        super.push(page);
        return "listSuccess";
    }

    public String toview(){
        Module module = moduleService.get(Module.class, this.module.getId());
        super.push(module);
        return "toViewSuccess";
    }

    public String tocreate(){
        List<Module> modules = moduleService.find("from Module", Module.class, null);
        super.put("modules",modules);
        return "toCreateSuccess";
    }

    public String insert(){
        moduleService.saveOrUpdate(module);
        return "insertSuccess";
    }

    public String toupdate(){
        Module module = moduleService.get(Module.class, this.module.getId());
        super.push(module);

        List<Module> modules = moduleService.find("from Module", Module.class, null);
        super.put("modules",modules);

        return "toUpdateSuccess";
    }

    public String update(){
        Module m = moduleService.get(Module.class, this.module.getId());
        m.setName(module.getName());
        m.setLayerNum(module.getLayerNum());
        m.setCpermission(module.getCpermission());
        m.setCurl(module.getCurl());
        m.setCtype(module.getCtype());
        m.setState(module.getState());
        m.setBelong(module.getBelong());
        m.setCwhich(module.getCwhich());
        m.setRemark(module.getRemark());
        m.setOrderNo(module.getOrderNo());
        m.setParentId(module.getParentId());
        m.setParentName(moduleService.get(Module.class,module.getParentId()).getName());

        moduleService.saveOrUpdate(m);

        return "updateSuccess";
    }

    public String delete(){
        moduleService.deleteById(Module.class,module.getId());
        return "deleteSuccess";
    }

    public void setModuleService(ModuleService moduleService) {
        this.moduleService = moduleService;
    }

    public Page<Module> getPage() {
        return page;
    }

    @Override
    public Module getModel() {
        return module;
    }
}
